#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"


void GM720_Enable_Flash_Control_and_SPI_Flash_En(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
//	u16 tmp=0;
	u8 buffer1[3];	
	u8 RM67298_Enable_Flash_Control[4] = {0xfe, 0x20, 0xf3, 0x02};
	u8 RM67298_Enable_Flash_Read_REG[3] = {0xEA,0xEB,0xEC};
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	for(i=0;i<sizeof(RM67298_Enable_Flash_Control);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM67298_Enable_Flash_Control[i];
		buffer1[2] = RM67298_Enable_Flash_Control[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
	for( i = 0; i< 3;i++)			 	
	{  	
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package                           
		buffer1[0] = 0x01;
		buffer1[1] = RM67298_Enable_Flash_Read_REG[i];
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(6);
		buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
	}	
	delay_ms(5);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}
void GM720_Inter_Demura_Ram(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM67298_Inter_Demura_Ram_Cmd_Table[26]={0xfe,0x40,0x0e,0x80,0xfe,0x20,0xb1,0x00,0xfe,0xd0,0x55,0x30,0x07,0x01,0xfe,0x40,0x0e,0x50,0xfe,0xd0,0x42,0x81,0x27,0x55,0xfe,0x00};	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
			
	for(i=0;i<sizeof(RM67298_Inter_Demura_Ram_Cmd_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM67298_Inter_Demura_Ram_Cmd_Table[i];
		buffer1[2] = RM67298_Inter_Demura_Ram_Cmd_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(15);
		i+=1;
	}	

	buffer[0] = 0x2F;
	buffer[1] = 0x01;		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);        																					
}
                                    
void GM720_Exit_Demura_Ram(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM67298_Exit_Demura_Ram_Cmd_Table[24]={0xfe,0x40,0x0e,0x80,0xfe,0x20,0xb1,0x00,0xfe,0xd0,0x55,0x00,0x07,0x01,0xfe,0x40,0x0e,0x50,0xfe,0xd0,0x42,0x01,0x27,0x55};
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	for(i=0;i<sizeof(RM67298_Exit_Demura_Ram_Cmd_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM67298_Exit_Demura_Ram_Cmd_Table[i];
		buffer1[2] = RM67298_Exit_Demura_Ram_Cmd_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}	
	buffer[0] = 0x2F;
	buffer[1] = 0x02;		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}
                               
void GM720_Flash_Write_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
//		u32 error_times=0;
		u8 buffer1[3];
		u8 RM67298_Flash_Write_Demura_data_Table[20]={0xfe,0x20,0xf2,0x04,0xb1,0x00,0xab,0x00,0xac,0x00,0xad,0x00,0xae,0x1a,0xaf,0xb3,0xb0,0x62,0xf2,0x10};
	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);	
		
		if(OLED.H_pixel == 1080 && OLED.V_pixel == 2160)	//599 601 inch
		{
			RM67298_Flash_Write_Demura_data_Table[13] = 0x1a;
			RM67298_Flash_Write_Demura_data_Table[15] = 0xb3;
			RM67298_Flash_Write_Demura_data_Table[17] = 0x62;
			//u8 RM67298_Flash_Write_Demura_data_Table[20]={0xfe,0x20,0xf2,0x04,0xb1,0x00,0xab,0x00,0xac,0x00,0xad,0x00,0xae,0x1a,0xaf,0xb3,0xb0,0x62,0xf2,0x10};
		}		
		
		for(i=0;i<sizeof(RM67298_Flash_Write_Demura_data_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM67298_Flash_Write_Demura_data_Table[i];
			buffer1[2] = RM67298_Flash_Write_Demura_data_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
		do
		{                        
				buffer1[0] = 0x01;
				buffer1[1] = 0xE8;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
				buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);   
				delay_ms(100);
		}while((buffer[4]&0x80)!=0x80);
		delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x03;		
		buffer[4] = 0x80;				

	error_tips0:
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);		
}                       

void GM720_Flash_Write_Demura2(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
}


void GM720_Flash_Check_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
//	u16 tmp=0;
	u8 buffer1[3];
	u8 RM67298_Flash_Check_Read_REG[2]={0xE6,0xE7};
	u8 RM67298_Flash_Check_Demura_data_Table[18]={0xfe,0x20,0xf2,0x04,0xab,0x00,0xac,0x00,0xad,0x00,0xae,0x1a,0xaf,0xb3,0xb0,0x62,0xf2,0x01};	
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	
	if(OLED.H_pixel == 1080 && OLED.V_pixel == 2160)	//599 601 inch
	{
		RM67298_Flash_Check_Demura_data_Table[11] =0x1a;
		RM67298_Flash_Check_Demura_data_Table[13] =0xb3;
		RM67298_Flash_Check_Demura_data_Table[15] =0x62;
		//u8 RM67298_Flash_Check_Demura_data_Table[18]={0xfe,0x20,0xf2,0x04,0xab,0x00,0xac,0x00,0xad,0x00,0xae,0x1a,0xaf,0xb3,0xb0,0x62,0xf2,0x01};
	}	
	
	for(i=0;i<sizeof(RM67298_Flash_Check_Demura_data_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM67298_Flash_Check_Demura_data_Table[i];
		buffer1[2] = RM67298_Flash_Check_Demura_data_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}	
	delay_ms(200);
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);		
	for( i = 0;i<sizeof(RM67298_Flash_Check_Read_REG);i++)			 	
	{  	
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package                           
		buffer1[0] = 0x01;
		buffer1[1] = RM67298_Flash_Check_Read_REG[i];
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
		buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
	}			

	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x04;					
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  	
}
                                  
void GM720_Flash_Erase_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM67298_Flash_Erase_Demura_data_Table[8]={0xfe,0x20,0xf2,0x04,0xb1,0x00,0xf3,0x01};
	u8 RM67298_Read_REG=0xE8;	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
		
	for(i=0;i<sizeof(RM67298_Flash_Erase_Demura_data_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM67298_Flash_Erase_Demura_data_Table[i];
		buffer1[2] = RM67298_Flash_Erase_Demura_data_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
	do
	{                        
		buffer1[0] = 0x01;
		buffer1[1] = RM67298_Read_REG;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(6);
		buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);      
	}while((buffer[4]&0x80)!=0x80);
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x05;		
	buffer[4] = 0x80;	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú	
}

void GM720_Flash_Demura_OTP(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
#if 1
	  u8 buffer1[3];
		u8 RM67298_Flash_Demura_OTP_Table[18]={0xfe,0x00,0x28,0x00,0x10,0x00,0xfe,0x40,0xf8,0x02,0xf2,0x01,0xf3,0xa5,0xf4,0x5a,0xf5,0x3c};
//		u8 Read_REG=0xE8;
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);

		for(i=0;i<sizeof(RM67298_Flash_Demura_OTP_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM67298_Flash_Demura_OTP_Table[i];
			buffer1[2] = RM67298_Flash_Demura_OTP_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}
    delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x09;

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
#endif
}

void GM720_Demura_Function_ON(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u8 RM67298_Demura_Function_ON_Table[10]={0xfe,0x20,0x09,0x10,0xA6,0xC0,0xA7,0xFF,0xBC,0x01};
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	
	
	for(i=0;i<sizeof(RM67298_Demura_Function_ON_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM67298_Demura_Function_ON_Table[i];
		buffer1[2] = RM67298_Demura_Function_ON_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x07;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú
}

void GM720_Demura_Function_OFF(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u8 RM67298_Demura_Function_OFF_Table[10]={0xfe,0x20,0x09,0x10,0xA6,0xC0,0xA7,0xFF,0xBC,0x81};
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
//297 298 299
	for(i=0;i<sizeof(RM67298_Demura_Function_OFF_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM67298_Demura_Function_OFF_Table[i];
		buffer1[2] = RM67298_Demura_Function_OFF_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}
	
	delay_ms(5);
	buffer[0] = 0x2F;  //Demura_type
	buffer[1] = 0x08;				
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}

void ProcessForDmuR04( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      //写寄存器
					GM720_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //读寄存器
					GM720_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //读寄存器
					GM720_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      //读寄存器
					GM720_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      //读寄存器
					GM720_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      //check flash ID
					GM720_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      //check flash ID
					GM720_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      //check flash ID
					GM720_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			case 0x0A:
					GM720_Flash_Write_Demura2(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			default:
				break;												
	}
}
//********************************************************************************************